package com.nchuoj.question.listener;

import com.feign.feign.requests.AnswerFeign;
import com.feign.message.Question2JudgeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Payload;

import java.util.Optional;

/**
 * @author lenovo
 */
@Configuration
@Slf4j
public class QuestionMqListener {

    private final AnswerFeign answerFeign;

    @Autowired
    public QuestionMqListener(AnswerFeign answerFeign) {
        this.answerFeign = answerFeign;
    }

    @RabbitListener(queues = {"judge2QuestionQueue"})
    public void getMessage(@Payload Question2JudgeMessage message) {
        Optional.ofNullable(message).ifPresent(answerFeign::recordAnswer);
    }

}
